export * from '@/api/system/Warehouse/index'
import request from '@/config/axios'

// 仓库与金料相关 API 定义
export interface WarehouseVO {
  id: number
  name: string
  type?: number // 1 公司仓 2 团队仓 3 人员仓
  balance?: number
  responsiblePerson?: string
  lastOperationTime?: string
  teamWarehouseId?: number // 团队仓ID（用于个人仓绑定）
}

export interface WarehousePageReq {
  pageNo: number
  pageSize: number
  name?: string
  type?: number | null
  types?: number[] // 支持多个类型筛选
}

export interface OperationRecordVO {
  id: number
  createTime: string
  operationType: 1 | 2 // 1 入料 2 出料
  operationSubtype: number
  operationWeight: number
  initialWeight: number
  finalWeight: number
  operatorName?: string
  relatedBusinessNo?: string
  subject?: string
  remark?: string
  attachmentUrl?: string | null
  warehouseIn?: string | null
  warehouseOut?: string | null
}

export interface OperationStatsVO {
  todayIn: number
  todayOut: number
  balance: number
}

// 分页查询：商家户（仓库）
export const getWarehousePage = (params: WarehousePageReq) => {
  return request.get({ url: '/gold/warehouse/page', params })
}

// 分页查询：销售户（人员）
export const getSalesUserPage = (params: { pageNo: number; pageSize: number; name?: string }) => {
  return request.get({ url: '/gold/sales/page', params })
}

// 仓库精简列表
export const getSimpleWarehouseList = (): Promise<WarehouseVO[]> => {
  return request.get({ url: '/gold/warehouse/simple-list' })
}

// 查询调拨仓库选项
export const getTransferWarehouseOptions = (
  excludeWarehouseId?: number,
  warehouseTypes?: number[]
): Promise<WarehouseVO[]> => {
  const params: any = {}
  if (excludeWarehouseId !== undefined) {
    params.excludeWarehouseId = excludeWarehouseId
  }
  if (warehouseTypes && warehouseTypes.length > 0) {
    params.warehouseTypes = warehouseTypes.join(',')
  }
  return request.get({
    url: '/gold/warehouse/transfer-options',
    params
  })
}

// 人员精简列表（用于派发接收人选择）
export const getSimpleUserListForGold = (): Promise<{ id: number; name: string }[]> => {
  return request.get({ url: '/gold/user/simple-list' })
}

// 上传地址（如果后端已有统一上传端点，也可直接使用此常量）
export const getUploadUrl = () => {
  const baseURL = (import.meta as any).env.VITE_BASE_URL + (import.meta as any).env.VITE_API_URL
  return `${baseURL}/infra/file/upload`
}

// 买料入料
export const createBuyIn = (data: {
  warehouseId: number
  weight: number // 操作克重，后端接收为BigDecimal
  operationSubtype: number
  creator: string
  remark?: string
  attachmentUrl?: string
}) => {
  return request.post({ url: '/gold/material/record/buy-in', data })
}

// 销售回料
export const createSalesReturn = (data: {
  warehouseId: number
  weight: number // 操作克重，后端接收为BigDecimal
  operationSubtype: number
  creator: string
  remark?: string
  attachmentUrl?: string
}) => {
  return request.post({ url: '/gold/material/record/sales-return', data })
}

// 出料/减料
export const createOutbound = (data: {
  warehouseId: number
  operationSubtype: number // 6-调拨出料，7-派发出料，8-采购出料
  weight: number // 操作克重，后端接收为BigDecimal
  creator: string
  remark?: string
  attachmentUrl?: string
}) => {
  return request.post({ url: '/gold/material/record/reduce', data })
}

// 调拨
export const createTransfer = (data: {
  transferType: number // 1-调拨，2-派发
  fromWarehouseId: number
  toWarehouseId: number
  transferWeight: number
  transferReason?: string
  creator: string
  remark?: string
  attachmentUrl?: string
}) => {
  return request.post({ url: '/gold/material/transfer/transfer', data })
}

// 派发
export const createDispatch = (data: {
  transferType: number // 1-调拨，2-派发
  fromWarehouseId: number
  toWarehouseId: number
  transferWeight: number
  transferReason?: string
  creator: string
  remark?: string
  attachmentUrl?: string
  teamWarehouseId?: number // 团队仓ID
}) => {
  return request.post({ url: '/gold/material/transfer/dispatch', data })
}

// 还料（从人员或外部归还至仓库）
export const createReturn = (data: {
  fromWarehouseId: number
  toWarehouseId: number
  transferWeight: number
  transferType: number
  transferReason?: string
  creator: string
  remark?: string
  attachmentUrl?: string
}) => {
  return request.post({ url: '/gold/material/transfer/return', data })
}

// 流水分页
export const getOperationRecordPage = (params: {
  pageNo: number
  pageSize: number
  warehouseId?: number
}) => {
  return request.get({ url: '/gold/material/record/page', params })
}

// 仓库或主体的指标统计
export const getOperationStats = (params: { warehouseId?: number }) => {
  return request.get({ url: '/gold/material/record/stats', params })
}

// 全局统计
export const getGlobalStats = () => {
  return request.get({ url: '/gold/material/record/global-stats' })
}

// 创建仓库
export const createWarehouse = (data: {
  name: string
  type: number
  responsiblePerson?: string
  remark?: string
}) => {
  return request.post({ url: '/gold/warehouse/create', data })
}
